app.controller('vipRedPageCtrl',
    function($ionicBackdrop, $ionicConfig, $rootScope, $interval, $myconfig, $http, $scope, $state, $ionicSideMenuDelegate, $ionicHistory, $ionicPopup, BthSevice, PopupService, $stateParams) {

        $scope.userMes = JSON.parse(localStorage.getItem('user'));
        $scope.curren = $scope.userMes.bonusbalance;
        //设置页面加载方式
        $scope.vipRedif = {
            hasmore_vipRed: false,
            page: 1,
            limit: 10
        };

        //返回
        $scope.gored = function() {
            if (!$stateParams.redid) {
                $state.go('app.vip');
            } else if ($rootScope.gored) {
                $state.go('app.red', { id: $stateParams.redid });
            }
        };

        //预置显示数据空数组
        $scope.vipRedListData = [];
        //请求接口获取列表内容
        $scope.vipRedList = function(id) {
            // if(ref){
            //     $scope.vipRedif.page = ref;
            //     $scope.vipRedListData = [];
            // }
            $http.get($myconfig.api.base_host + '/redpacketlogs/list?page=' + $scope.vipRedif.page + '&limit=' + $scope.vipRedif.limit + '&user_id=' + id)
                .then(function(res) {
                    // 判断请求下的数据的条数
                    if (res.data.data.length < 10) {
                        // 如果请求下的数据不足十条，则不进行下次请求
                        $scope.vipRedif.hasmore_vipRed = true;
                        // 如果请求的数据大于0，则将数据拼接至原有数据之后
                        if (res.data.data.length > 0) {
                            //数据拼接
                            $scope.vipRedListData = $scope.vipRedListData.concat(res.data.data);
                        }
                        // console.log(res.data.data);
                        return;
                    }
                    // console.log(res.data.data);
                    $scope.vipRedListData = $scope.vipRedListData.concat(res.data.data);
                    $scope.vipRedif.page += 1;
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                });
        };



        //自执行请求
        if ($scope.userMes) {
            $scope.vipRedList($scope.userMes.id);
        }

        $scope.$on('$ionicView.enter', function() {

            $ionicConfig.views.swipeBackEnabled(false);

        });


    });